package com.dawnClass.service.impl;

import com.dawnClass.mapper.ConsumerMapper;
import com.dawnClass.pojo.Consumer;
import com.dawnClass.pojo.ConsumerExample;
import com.dawnClass.service.ConsumerService;
import com.dawnClass.util.AESUtil;
import com.dawnClass.util.RSAUtils;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.List;
@Service
public class ConsumerServiceImpl implements ConsumerService {

    private static KeyPair keyPair;

    @Autowired
    private ConsumerMapper consumerMapper;

    @Override
    public ServiceResponse getByConsumerNameAndConsumerPassword(Long consumerTel, String consumerPassword) {
        System.out.println("consumerPassword:"+consumerPassword);
//        try {
//            consumerPassword = RSAUtils.encrypt(consumerPassword,RSAUtils.getPrivateKey());
//            System.out.println("RSAUtils.getPrivateKey():"+RSAUtils.getPrivateKey());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        System.out.println("登录密码:"+consumerPassword);

        String encrypted = consumerPassword;
        String privateKey = getPrivateKey1();
        System.out.println(privateKey);

        try {
            System.out.println("RSA解密中.....");
            String decrypted = RSAUtils.privateDecrypt(encrypted, RSAUtils.getPrivateKey(privateKey));

            System.out.println("RSA解密后：" + decrypted);
            String encrypt =  AESUtil.encrypt(decrypted);

            System.out.println("AES加密后：" + encrypt);
            consumerPassword = encrypt;

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("consumerPassword:"+consumerPassword);
        ConsumerExample example = new ConsumerExample();
        example.createCriteria().andConsumerTelEqualTo(consumerTel);
        List<Consumer> consumerList = consumerMapper.selectByExample(example);
        if (consumerList.size()>0){
            Consumer consumer = consumerList.get(0);
            String getPassword = consumer.getConsumerPassword();
            System.out.println("数据库读取到的密码:"+getPassword);
                if(consumerPassword.equals(getPassword)){
                    return ServiceResponse.getSuccess(consumer);
                }
        }

        return ServiceResponse.getFail(null);
    }

    @Override
    public ServiceResponse getPublicKey1() {
        try {
            keyPair = RSAUtils.genKeyPair(1024);
            PublicKey publicKey=keyPair.getPublic();
            System.out.println("公钥：");
            String pubKey= new String(Base64.getEncoder().encode(publicKey.getEncoded()));
            System.out.println(pubKey);
            return ServiceResponse.getSuccess(pubKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResponse.getFail(null);
    }

    @Override
    public String getPrivateKey1() {
        try {
            PrivateKey privateKey=keyPair.getPrivate();
            System.out.println("get私钥：");
            String priKey = new String(Base64.getEncoder().encode(privateKey.getEncoded()));
            System.out.println(priKey);
            return priKey;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}
